package day20171012;

import java.util.Scanner;
import java.util.ArrayList;

public class LibrarySystem {
    public static void main(String[] args) {
        int num;
        Scanner sc = new Scanner(System.in);
        ArrayList<String> bookName = new ArrayList<>();
        ArrayList<String> bookNumber = new ArrayList<>();
        ArrayList<String> bookStatus = new ArrayList<>();
        ArrayList<String> bookPrice = new ArrayList<>();

        while (true) {
            System.out.println();
            System.out.println("    四川旅游学院图书管理系统.");
            System.out.println("---1.添加图书.");
            System.out.println("---2.查找图书.");
            System.out.println("---3.删除图书.");
            System.out.println("---4.显示所有图书.");
            System.out.println("---5.退出程序.");
            System.out.println("---6.借出图书.");
            System.out.println("---7.归还图书.");
            System.out.println("---请输入你的选择.");
            System.out.println("--------------------");
            num = sc.nextInt();
            if (num == 5) {
                System.out.println("程序结束.");
                break;
            }

            switch (num) {
                case 1:
                    addBook(bookName, bookNumber, bookPrice, bookStatus);
                    break;
                case 2:
                    lookBook(bookName, bookNumber, bookPrice, bookStatus);
                    break;
                case 3:
                    deleteBook(bookName, bookNumber, bookPrice, bookStatus);
                    break;
                case 4:
                    PrintBook(bookName, bookNumber, bookPrice, bookStatus);
                    break;
                case 6:
                    borrowBook(bookName, bookStatus, bookNumber);
                    break;
                case 7:
                    returnBook(bookName, bookNumber, bookStatus);
                    break;
                default:
                    System.out.println("无此选项");
                    break;
            }
        }
    }

    static void addBook(ArrayList booKName, ArrayList bookNumber, ArrayList bookPrice, ArrayList bookStatus) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入图书名称.");
        String name = s.nextLine();
        booKName.add(name);

        System.out.println("请输入图书编号.");
        String numeber = s.nextLine();
        bookNumber.add(numeber);

        System.out.println("请输入图书价格.");
        int price = s.nextInt();
        bookPrice.add(price);

        bookStatus.add("在库");

        if (name != null && numeber != null) {
            System.out.println("图书添加成功.!");
        }
    }

    static void lookBook(ArrayList bookName, ArrayList bookNumber, ArrayList bookPrice, ArrayList bookStatus) {
        Scanner in = new Scanner(System.in);
        Scanner ch = new Scanner(System.in);

        if (bookName.size() == 0) {
            System.out.println("图书库暂无图书,请先添加图书.");
        } else {
            System.out.println("请选择图书查找方式.");
            System.out.println("1---按图书名称查找.");
            System.out.println("2---按图书编号查找.");
            int choose = in.nextInt();

            while (choose == 1) {
                System.out.println("请输入图书的名称.");
                String name = ch.nextLine();
                if (bookName.contains(name) == false) {
                    System.out.println("图书库暂未添加此图书.");
                    break;
                }

                int location = -1;
                System.out.println("图书名称" + " " + "图书编号" + " " + "图书价格" + " " + "图书状态");
                for (int i = 0; i < bookName.size(); i++) {
                    if (name.equals(bookName.get(i))) {
                        location = i;

                        System.out.println(bookName.get(location) + "    " + bookNumber.get(location) + "     " + bookPrice.get(location) + "       " + bookStatus.get(i));
                    }
                }
                choose = 0;
            }

            while (choose == 2) {
                System.out.println("请输入图书编号.");
                String number = ch.nextLine();
                if (bookNumber.contains(number) == false) {
                    System.out.println("图书库暂未添加此图书.");
                    break;
                }

                int location = -1;
                System.out.println("图书名称" + " " + "图书编号" + " " + "图书价格" + " " + "图书状态");
                for (int i = 0; i < bookNumber.size(); i++) {
                    if (number.equals(bookNumber.get(i))) {
                        location = i;
                        System.out.println(bookName.get(location) + "      " + bookNumber.get(location) + "      " + bookPrice.get(location) + "    " + bookStatus.get(i));
                    }
                }
                choose = 0;
            }
        }
    }

    static void deleteBook(ArrayList bookName, ArrayList bookNumber, ArrayList bookPrice, ArrayList bookStatus) {
        Scanner sc = new Scanner(System.in);
        if (bookName.size() == 0) {
            System.out.println("图书库暂无图书,请先添加图书.");
        } else {
            System.out.println("请输入要删除的图书的名称.");
            String name = sc.nextLine();
            if (bookName.contains(name) == false) {
                System.out.println("此图书暂未添加.");
            } else {
                int location = -1;
                for (int i = 0; i < bookName.size(); i++) {
                    if (name.equals(bookName.get(i))) {
                        bookName.remove(bookName.get(i));
                        location = i;
                        bookNumber.remove(location);
                        bookPrice.remove(location);
                        bookStatus.remove(location);
                    }
                }
                System.out.println("图书已从图书库删除.");
            }
        }
    }

    static void PrintBook(ArrayList bookName, ArrayList bookNumber, ArrayList bookPrice, ArrayList bookStatus) {
        if (bookName.size() == 0) {
            System.out.println("暂未添加任何图书，请先添加图书.");
        } else {
            System.out.println("图书名称" + "   " + "图书编号" + "  " + "图书价格" + " " + "图书状态 ");
            for (int i = 0; i < bookName.size(); i++) {
                System.out.println(bookName.get(i) + "   " + bookNumber.get(i) + "   " + bookPrice.get(i) + "   " + bookStatus.get(i));
            }
        }
    }

    static void borrowBook(ArrayList bookName, ArrayList bookStatus, ArrayList bookNumber) {
        Scanner ch = new Scanner(System.in);
        if (bookName.size() == 0) {
            System.out.println("图书库暂无图书，请先添加图书.");
        } else {
            System.out.println("请输入要借图书的名称.");
            String name = ch.nextLine();
            System.out.println("请输入要借图书的编号.");
            String number = ch.nextLine();

            if (bookName.contains(name) == false) {
                System.out.println("此图书不在库中.");
            } else {
                int location = -1;
                for (int i = 0; i < bookNumber.size(); i++) {
                    if (number.equals(bookNumber.get(i))) {
                        location = i;
                        bookStatus.set(location, "借出");
                    }
                }
                System.out.println("成功借出!");
            }
        }
    }

    static void returnBook(ArrayList bookName, ArrayList bookNumber, ArrayList bookStatus) {
        Scanner ch = new Scanner(System.in);
        if (bookName.size() == 0) {
            System.out.println("图书库暂无图书，请先添加图书.");
        } else {
            System.out.println("请输入要归还的图书的名称.");
            String name = ch.nextLine();
            System.out.println("请输入要归还图书的编号.");
            String number = ch.nextLine();
            int location = -1;

            for (int i = 0; i < bookNumber.size(); i++) {
                if (number.equals(bookNumber.get(i))) {
                    location = i;
                    bookStatus.set(location, "在库");
                }
            }
            System.out.println("成功归还.");
        }
    }
}